<?php

use frontend\modules\common\models\BaseParam;
use yii\grid\GridView;
use yii\widgets\Pjax;
use yii\widgets\LinkPager;
use yii\helpers\Html;
use yii\helpers\Url;

/* @var $this yii\web\View */
/* @var $searchModel frontend\modules\resource\models\BasMemberSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */

$this->title = '用户列表';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="bas-member-index">

    <?php Pjax::begin(['id' => 'centerView']); ?>
    <div class="searchBody">
        <?= $this->render('_search', ['model' => $searchModel]); ?>
        <div class="addBtn">
            <?= Html::button('导入数据', ['class' => 'btn btn-primary daoru']) ?>
            <?= Html::button('下载模板', ['class' => 'btn btn-primary muban']) ?>
        </div>
    </div>

    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'columns' => [
            'avatar' => [
                'attribute' => 'avatar',
                'format' => [
                    'image',
                    [
                        'width' => 'auto%',
                        'height' => '60px',
                        'style' => 'display: block; margin: 0 auto;',
                    ]
                ],
                'value' => 'avatar'
            ],
            'name',
            'id_number',
            'phone',
            'user_name',
            'created_at',
            'userStatus.status' => [
                'attribute' => 'user_status',
                'value' => function ($model) {
                    return BaseParam::CodeHelper(BaseParam::$userBranchStatus, $model->userStatus->status);

                }
            ],
            //功能
            [
                'class' => 'frontend\modules\common\models\ActionJsColumn',
                'template' => '{detail}{user-status}{delete}',
                'contentOptions' => ['style' => 'max-width: 280px;', 'width' => '280px'],
            ]
        ],
        'pager' => [
            'class' => LinkPager::className(),
            'firstPageLabel' => '首页',
            'prevPageLabel' => '《',
            'nextPageLabel' => '》',
            'lastPageLabel' => '尾页',
            //            'goPageLabel' => true,
            //            'totalPageLable' => '共x页',
            //            'goButtonLable' => 'GO',
            'maxButtonCount' => 5,
            'hideOnSinglePage' => false,
        ]
    ]); ?>
    <?php Pjax::end(); ?>


</div>
<div id="open" style="display: none;">
    <div style="text-align: center; margin-top: 20px;margin-left: 20px;display: flex;">
        <input type="file" id="file" class="fileInput">
        <input type="hidden" id="file_value" value="">
    </div>
    <div style="display: flex;justify-content: center;margin-top: 20px">
        <span id="start_daoru" class='btn btn-primary' type="button">开始导入</span>
    </div>
</div>
<!--增删改 操作-->
<?php echo $this->render('../../../common/views/base/_column', ['option' => ['d', 'v']]); ?>
<script>
    <?php $this->beginBlock('js_ok_remove') ?>
    //显示隐藏
    $(document).on('click', '.user-status', function () {
        var id = $(this).closest('tr').data('key');

        $.ajax({
            type: "POST",
            url: '<?=Url::toRoute('user-status')?>',
            data: {id: id},
            dataType: 'json'
        })
            .done(function (msg) {
                console.log(msg);
                if (msg['status'] == 200) {
                    layer.msg(msg['msg'], {icon: 1, time: 500}, function () {
                        $.pjax.reload({container: "#centerView"});
                    });
                } else {
                    layer.msg(msg['msg'], {icon: 2});
                }
            });
    });

    var down = true;
    //下载模板
    $(document).on('click', '.muban', function () {
        if (down) {
            down = !down;
            window.location.href = '/resource/branch-user/template?type=user';
            down = !down;
        }
    });
    //导入数据
    $(document).on('click', '.daoru', function () {
        layer.open({
            type: 1,
            title: "导入用户信息",
            area: ["350px", "160px"],
            content: $('#open'),
        });
    });
    //上传
    $(document).on('change', '#file', function () {
        console.log('上传');
        var excel_file = $(this);
        var fd = new FormData();
        fd.append('excel', excel_file[0].files[0]);

        var up = layer.msg('上传中', {
            icon: 16
            , shade: 0.01
        });
        $.ajax({
            url: '/resource/branch-user/upload-file',
            type: 'POST',
            data: fd,
            processData: false,
            contentType: false,
            dataType: "json",
            success: function (msg) {

                if (msg.status == 200) {
                    layer.msg(msg['msg'], {icon: 1, time: 1000}, function () {
                        $('#file_value').val(msg['data']);
                    });
                } else {
                    layer.msg(msg['msg'], {icon: 2, time: 1000});
                }
            },
            error: function () {
            },
            complete: function () {
                layer.close(up);
            }
        })
    });
    //开始导入
    $(document).on('click', '#start_daoru', function () {
        var file = $('#file_value').val();
        if (!file) {
            layer.msg('没有导入数据', {icon: 2});
            return false;
        }
        $.ajax({
            type: "POST",
            url: '<?=Url::toRoute('import-excel')?>',
            data: {file: file},
            dataType: 'json'
        })
            .done(function (msg) {
                console.log(msg);
                if (msg['status'] == 200) {
                    layer.msg(msg['msg'], {icon: 1, time: 1000}, function () {
                        $.pjax.reload({container: "#centerView"});
                        layer.closeAll();
                    });
                } else {
                    layer.msg(msg['msg'], {icon: 2});
                }
            });
    });

    <?php $this->endBlock() ?>
</script>
<?php $this->registerJs($this->blocks['js_ok_remove'], \yii\web\View::POS_END); ?>

